//
//  CJAssetTrendCell.m
//  Boss
//
//  Created by admin on 2018/9/18.
//  Copyright © 2018年 admin. All rights reserved.
//

#import "CJAssetTrendCell.h"
#import "Boss-Bridging-Header.h"

@interface CJAssetTrendCell()<ChartViewDelegate>
@property (nonatomic,strong)LineChartView *chartView;
//@property (nonatomic,strong)LineChartView *linechartView;
@property (strong, nonatomic) IBOutlet UIView *LineView;
@property (strong, nonatomic) IBOutlet UISegmentedControl *dateSegmented;

@end

@implementation CJAssetTrendCell

-(void)setThirty:(CJAssetsThirty *)thirty
{
    _thirty = thirty;
    
    [self setXArray:thirty.thirtyAmount WithYArray:thirty.thirtyTimes];
}
-(void)setSeven:(CJAssetsSeven *)seven
{
    _seven = seven;
    
    [self setXArray:seven.sevenTimes WithYArray:seven.sevenAmount];
    
}

-(void)setXArray:(NSArray *)XArray WithYArray:(NSArray *)YArray
{
    NSMutableArray *values = [[NSMutableArray alloc] init];
    NSMutableArray *colorArray= [[NSMutableArray alloc]init];
    for (int i = 0; i < XArray.count; i++)
    {
        
        double val = [YArray[i] floatValue];
        [values addObject:[[ChartDataEntry alloc] initWithX:i y:val icon: [UIImage imageNamed:@"icon"]]];
        UIColor *color = [UIColor clearColor];
        [colorArray addObject:color];
    }
    LineChartDataSet *set1 = nil;
    if (_chartView.data.dataSetCount > 0)
    {
        set1 = (LineChartDataSet *)_chartView.data.dataSets[0];
        set1.values = values;
        [_chartView.data notifyDataChanged];
        [_chartView notifyDataSetChanged];
    }
    else
    {
        
        set1 = [[LineChartDataSet alloc] initWithValues:values label:@"DataSet 1"];
        set1.mode = LineChartModeHorizontalBezier;
        set1.cubicIntensity = 0.2;
        set1.drawCirclesEnabled = NO;
        set1.lineWidth = 1.8;
        set1.circleRadius = 4.0;
        [set1 setCircleColor:UIColor.whiteColor];
        set1.highlightColor = [UIColor colorWithRed:244/255.f green:117/255.f blue:117/255.f alpha:1.f];
        [set1 setColor:UIColor.whiteColor];
        set1.fillColor = UIColor.whiteColor;
        set1.fillAlpha = 1.f;
        set1.drawHorizontalHighlightIndicatorEnabled = NO;
        set1.drawIconsEnabled = NO;
        set1.highlightEnabled = NO;
    //    [set1 setValueFormatter:[[CubicLineSampleFillFormatter alloc]init]];
        
        
        
        
        
        
        
        
        NSArray *gradientColors = @[
                                    (id)[ChartColorTemplates colorFromString:@"#00ff0000"].CGColor,
                                    (id)[ChartColorTemplates colorFromString:@"#ffff0000"].CGColor
                                    ];
        CGGradientRef gradient = CGGradientCreateWithColors(nil, (CFArrayRef)gradientColors, nil);
        
        set1.fillAlpha = 1.f;
        set1.fill = [ChartFill fillWithLinearGradient:gradient angle:90.f];
        set1.drawFilledEnabled = YES;
        
        CGGradientRelease(gradient);
        
        NSMutableArray *dataSets = [[NSMutableArray alloc] init];
        [dataSets addObject:set1];
        
        LineChartData *data = [[LineChartData alloc] initWithDataSets:dataSets];
        //data.xMax = 7;
        
        _chartView.data = data;
        
      //  XDateFormatter *xformater = [[XDateFormatter alloc]init];
      ///  xformater.xArray = XArray;
     //   ChartXAxis  *xAxis = _chartView.xAxis;
      //  [xAxis setValueFormatter:xformater];
        // [xAxis setAxisMaximum:7];
    }
    [self.chartView setVisibleXRangeMaximum:7];
    // [self.chartView moveViewToX:30.0f];
    [_chartView.data notifyDataChanged];
    [_chartView notifyDataSetChanged];
}





- (void)awakeFromNib {
    [super awakeFromNib];
    [self createLineChartView];
    [self setUpUI];
}
-(void)setUpUI
{
    self.dateSegmented.layer.borderColor = kColorMainColor.CGColor;
    self.dateSegmented.tintColor = kColorMainColor;
}
-(void)createLineChartView
{
    _chartView =[[LineChartView alloc]init];
    _chartView.frame = CGRectMake(10, 60, ScreenWidth - 20, 260);
    _chartView.legend.form = ChartLegendFormNone;
    //说明图标
    _chartView.dragEnabled = YES;
    //拖动手势
    _chartView.rightAxis.enabled = NO;//隐藏右Y轴
    _chartView.chartDescription.enabled = NO;//不显示描述label
    _chartView.doubleTapToZoomEnabled = NO; //禁止双击缩放
    _chartView.drawGridBackgroundEnabled = NO;
    _chartView.drawBordersEnabled= NO;
    
    _chartView.dragEnabled = YES; //拖动气泡
    [_chartView animateWithXAxisDuration:2.20  easingOption:ChartEasingOptionEaseOutBack];//加载动画时长
    
    [self.LineView addSubview:_chartView];
    _chartView.delegate = self;
    
    _chartView.chartDescription.enabled = NO;
    [_chartView setNoDataText:@""];
    [_chartView setDrawBordersEnabled:NO];
    _chartView.legend.enabled = NO;
    
    //_chartView.chartDescription.enabled = NO;
    
    
    
    
    _chartView.dragEnabled = YES;
    _chartView.scaleYEnabled = false;
    _chartView.scaleXEnabled = false;
    _chartView.pinchZoomEnabled = NO;
    _chartView.doubleTapToZoomEnabled = NO;
    // _chartView.exclusiveTouch = YES;
    
    [_chartView setScaleEnabled:YES];
    _chartView.pinchZoomEnabled = YES;
    _chartView.drawGridBackgroundEnabled = NO;
    
    // x-axis limit line
    ChartLimitLine *llXAxis = [[ChartLimitLine alloc] initWithLimit:10.0 label:@"Index 10"];
    llXAxis.lineWidth = 4.0;
    llXAxis.lineDashLengths = @[@(10.f), @(10.f), @(0.f)];
    llXAxis.labelPosition = ChartLimitLabelPositionRightBottom;
    llXAxis.valueFont = [UIFont systemFontOfSize:10.f];
    
    //[_chartView.xAxis addLimitLine:llXAxis];
    
    _chartView.xAxis.gridLineDashLengths = @[@10.0, @10.0];
    _chartView.xAxis.gridLineDashPhase = 0.f;
    
    //    ChartLimitLine *ll1 = [[ChartLimitLine alloc] initWithLimit:150.0 label:@"Upper Limit"];
    //    ll1.lineWidth = 4.0;
    //    ll1.lineDashLengths = @[@5.f, @5.f];
    //    ll1.labelPosition = ChartLimitLabelPositionRightTop;
    //    ll1.valueFont = [UIFont systemFontOfSize:10.0];
    
    //    ChartLimitLine *ll2 = [[ChartLimitLine alloc] initWithLimit:-30.0 label:@"Lower Limit"];
    //    ll2.lineWidth = 4.0;
    //    ll2.lineDashLengths = @[@5.f, @5.f];
    //    ll2.labelPosition = ChartLimitLabelPositionRightBottom;
    //    ll2.valueFont = [UIFont systemFontOfSize:10.0];
    //
    ChartYAxis *leftAxis = _chartView.leftAxis;
    [leftAxis removeAllLimitLines];
    ////    [leftAxis addLimitLine:ll1];
    ////    [leftAxis addLimitLine:ll2];
    //    leftAxis.axisMaximum = 200.0;
    //    leftAxis.axisMinimum = -50.0;
    //    leftAxis.gridLineDashLengths = @[@5.f, @5.f];
    //    leftAxis.drawZeroLineEnabled = NO;
    //    leftAxis.drawLimitLinesBehindDataEnabled = YES;
    leftAxis.labelTextColor = [UIColor grayColor];
    leftAxis.labelFont = [UIFont systemFontOfSize:12];
    leftAxis.drawGridLinesEnabled = YES;
    leftAxis.drawLabelsEnabled = YES;
    leftAxis.granularityEnabled = NO;
    leftAxis.drawZeroLineEnabled = NO;
    leftAxis.gridColor = [UIColor colorWithRed:230/255.0 green:230/255.0 blue:230/255.0 alpha:1.0];
   
//    YDateFormatter *ydate =  [[YDateFormatter alloc]init];
//    [leftAxis setValueFormatter:ydate];
    ChartXAxis  *xAxis = _chartView.xAxis;
    [xAxis setLabelPosition:XAxisLabelPositionBottom];
    xAxis.enabled = YES;
    xAxis.drawAxisLineEnabled = NO;
    xAxis.drawGridLinesEnabled = NO;
    xAxis.labelTextColor = [UIColor grayColor];
    xAxis.labelFont = [UIFont systemFontOfSize:12];
    //ChartYAxis *rightAxis = _chartView.
    _chartView.rightAxis.enabled = NO;
    // [self setDataCount:10 range:10];
    [_chartView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
        make.top.mas_offset(0);
        //make.top.equalTo(self.Threelabel.mas_bottom).offset(30);
    }];
    
}
- (IBAction)dateAction:(UISegmentedControl *)sender {
    
    
    NSLog(@"%@",sender);
    
    
    
    
    
    
    
    
}




















@end
